import { ResponseBodyBaseType } from 'src/typings';
import { Model } from 'mongoose';
import { successDelete } from 'src/common/responses';
import { HttpErrorException } from 'src/common/exceptions';

/**
 * 通过id删除
 * @param model 数据库表Model
 * @param ids ids
 * @param isFake 是否为虚假删除  默认为否
 * @returns 返回删除结果
 */
export const deleteByIds = async <M>(
  model: Model<M>,
  ids: string[],
  isFake = false,
) => {
  try {
    await Promise.all(
      ids.map((_id) => {
        if (isFake) {
          return model.updateMany({ _id }, { isDeleted: true });
        } else {
          return model.deleteMany({ _id });
        }
      }),
    );
    return successDelete();
  } catch (error: any) {
    throw new HttpErrorException(error.message);
  }
};
